/*
 * Author: nicolas.bredeche(@)lri.fr
 * Created on 11 d�c. 2006
 * 
 */

package picoevo.tutorials.simbad.robotavoidbehavior;

import picoevo.core.evolution.ParameterSet;
import picoevo.es.cmaes.WrapperForCMAES;
import picoevo.toolbox.Display;
import picoevo.toolbox.Misc;
import simbad.gui.Simbatch;

/**
 * @author nicolas
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class Main_SimpleAvoiderRobot_withCMAES {

	public static void main(String[] args) {

		// see cmaes wrapper tutorial "CMAESexample.java" in package
		// picoevo.tutorials.cmaes for code tutorial

		// load properties
		ParameterSet params = new ParameterSet("CMA-ES ParameterSet");
		params.loadProperties("picoevo/app/Simbad_AvoiderRobot/CMAEvolutionStrategy.properties");

		// set EvaluationFunction
		params.setProperty("EvaluationOperator", new CMAES_EvaluationOperator_SimpleAvoiderRobot(params)); // warning:
		// cmaes
		// only
		// minimize

		// create the simulation environment for evaluation purpose
		Display.info("Initialize evaluation environment.");
		params.setProperty("Robot", new Evaluator_SimpleAvoiderRobot());
		params.setProperty("Simulator", new Simbatch((Evaluator_SimpleAvoiderRobot) params.getObjectProperty("Robot"), true));
		((Simbatch) params.getObjectProperty("Simulator")).reset();

		// ## modify pop size and multi-start
		params.setProperty("populationSize", "10"); // =10 # AKA lambda - mu is
		// automaticaly computed as
		// lambda/2
		params.setProperty("numberOfRuns", "12"); // =12 # restart mode if > 1
		params.setProperty("stopFunctionValue", Double.NEGATIVE_INFINITY); // dismissing
		// STOPfunctionvalue
		// because
		// minimizing
		// (-F)
		// starts
		// from
		// zero
		// and
		// go
		// torwards
		// -INF
		// (no
		// known
		// lower
		// bound)

		// ## log files
		String logfilenameprefix = "log/logfile_cmaes_robotwallavoider_" + Misc.getCurrentTimeAsCompactString();
		params.displayInformation(logfilenameprefix + ".param");
		params.setProperty("outputFileNamesPrefix", logfilenameprefix + "_");

		// ### STEP 2 : INITIALISING and EVOLVING

		WrapperForCMAES myEvolvableWorld = new WrapperForCMAES("EvolvingWithCMA-ES", params);
		myEvolvableWorld.performInitialisation();
		myEvolvableWorld.evolve(); // perform full evolution (with restart, if
		// needed)

		// ### STEP 3 : DISPLAY THE BEST

		double[] bestgenome = myEvolvableWorld.bestSolution.getX();

		// ### dispose

		// ((Evaluator_SimpleAvoiderRobot)(params.getObjectProperty("Simulator"))).computeRobotFitnessTest(bestgenome);

		System.out.println("Display best solution (infinite loop).");

		Simbatch simulator = ((Simbatch) (params.getObjectProperty("Simulator")));
		Evaluator_SimpleAvoiderRobot robot = ((Evaluator_SimpleAvoiderRobot) (params.getObjectProperty("Robot")));
		robot.setGenome(bestgenome);

		while (true) // infinite loop
		{
			simulator.reset(); // stability test?
			robot._myRobot.resetEvaluation();
			int i = 0;
			int maxit = 10000;

			while (i < maxit && ((Evaluator_SimpleAvoiderRobot) params.getObjectProperty("Robot"))._isRunnable == true) {
				try {
					Thread.sleep(10);
				} catch (Exception e) {
				} // [!n] hack : make it visible...
				((Simbatch) params.getObjectProperty("Simulator")).step();
				i++;
			}

			if (i < maxit)
				System.out.println("robot crashed at step " + i + ".");
			else
				System.out.println("robot is alive after " + maxit + " steps.");
		}

		// code unreachable
		// ((Simbatch)params.getObjectProperty("Simulator")).dispose();
		// System.out.println("stopped.");

	}
}
